﻿@implements IDisposable

<MudGrid>
    <MudItem md="4">
        <MudButton OnClick="Get" Variant="Variant.Filled" StartIcon="@Icons.Custom.Uncategorized.Radioactive" Color="Color.Error">Get All Keys</MudButton>
    </MudItem>
    <MudItem md="4">
        <MudButton OnClick="Remove" Variant="Variant.Filled" StartIcon="@Icons.Custom.Uncategorized.Radioactive" Color="Color.Success">Remove Random Key</MudButton>
    </MudItem>
    <MudItem md="12">
        @string.Join(", ", Result)
    </MudItem>
</MudGrid>

@code {

    private string Key = "RandomKey";

    private List<string> Result = new();

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                await LocalStorage.SetAsync(Key, new Random().Next(1, 100));
            }
            catch (Exception ex)
            {
                Snackbar.Add($"{DashboardResource.StringError} : {ex.ToString()}", Severity.Error);
            }
        }
    }

    private async Task Get()
    {
        try
        {
            Result = await LocalStorage.KeysAsync();
        }
        catch (Exception ex)
        {
            Snackbar.Add($"{DashboardResource.StringError} : {ex.ToString()}", Severity.Error);
        }
    }

    private async Task Remove()
    {
        try
        {
            await LocalStorage.RemoveAsync(Key);
        }
        catch (Exception ex)
        {
            Snackbar.Add($"{DashboardResource.StringError} : {ex.ToString()}", Severity.Error);
        }
    }

    public async void Dispose() => await Dom.DisposeAsync();
}